package com.hao.tool.common.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionLikeType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.IOException;
import java.util.List;
import java.util.TimeZone;

/**
 * description
 *
 * @author RuiHaoZ on CMHK.fs
 * @date 2020-05-20 14:44
 */
@Slf4j
public class JsonKitUtil {

    private static ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        //允许单引号
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        //属性名称允许不带双引号
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        //忽略无法识别的属性
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 忽略无法转换的对象 “No serializer found for class com.xxx.xxx”
        //objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
        //允许将空字符串作为null处理
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
    }

    public static String getString(Object object) {
        String result = "";
        if (null == object) {
            return result;
        }
        try {
            result = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            log.warn("序列化失败:{}", ExceptionUtils.getMessage(e));
        }
        return result;
    }

    public static <T> T getObject(String src, TypeReference<T> valueType) {
        if (null == src || src.length() == 0) {
            return null;
        }

        T result = null;
        try {
            result = objectMapper.readValue(src, valueType);
        } catch (IOException e) {
            log.warn("反序列化失败:{}", ExceptionUtils.getMessage(e));
        }
        return result;
    }

    public static <T> T getObject(String src, Class<T> valueType) {
        if (null == src || src.length() == 0) {
            return null;
        }
        T result = null;
        try {
            result = objectMapper.readValue(src, valueType);
        } catch (IOException e) {
            e.printStackTrace();
            log.warn("反序列化失败:{}", ExceptionUtils.getMessage(e));
        }
        return result;
    }

    public <T> List<T> getList(String src, Class<T> valueType) {
        if (StringUtils.isBlank(src)) {
            src = "[]";
        }
        List<T> result = null;
        CollectionLikeType likeType = objectMapper.getTypeFactory().constructCollectionLikeType(List.class, valueType);
        try {
            result = objectMapper.readValue(src, likeType);
        } catch (IOException e) {
            log.warn("序列化失败:{}", ExceptionUtils.getMessage(e));
        }
        return result;
    }

    public static void main(String[] args) {

    }
}
